<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/23
 * Time: 16:49
 */

namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\OrganizeModel;
use app\portal\model\OrganizeChildModel;
use app\portal\model\UserOrganizeModel;
use app\portal\model\CommunityModel;
use app\portal\model\CommunityZoneModel;
use app\portal\model\CommunityDistrictModel;
use think\Db;


class AdminOrganizeController extends AdminBaseController
{
    //志愿服务岗位
    public function volunt_post(){
        $data=Db::name('volunt_post')->select();
        $this->assign('data',$data);
        return $this->fetch();
    }
    //添加、修改志愿服务岗位
    public function volunt_post_add(){
        $id=input('id');
        if($id){
            $data=Db::name('volunt_post')->find($id);
            $this->assign('data',$data);
        }
        $this->assign('id',$id);
        return $this->fetch();
    }
    //保存添加/修改志愿者服务岗位
    public function volunt_post_save(){
        $data=input('post.');
        if(empty($data['id'])){
            unset($data['id']);
            $result = Db::name('volunt_post')->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminOrganize/volunt_post'));
        }else{
            $result = Db::name('volunt_post')->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminOrganize/volunt_post'));
        }
    }
    //删除志愿者组织分支
    public function volunt_post_del(){
        $id= $this->request->param('id');
        $result=Db::name('volunt_post')->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //志愿者组织列表
    public function index(){
        $db=new OrganizeModel();
        $data=$db->select();
        $this->assign('data',$data);

        return $this->fetch();
    }
    //添加/修改志愿者组织页面
    public function add(){
        $Model = new OrganizeModel();
        $id=input('id');
        if($id){
            $data=$Model->find($id);
            $this->assign('data',$data);
        }
        $this->assign('id',$id);
        return $this->fetch();
    }
    //保存添加/修改志愿者数据
    public function addPost(){
        $Model = new OrganizeModel();
        $data=input('post.');
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminOrganize/index'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminOrganize/index'));
        }

    }

    //区县列表
    public function district(){
       $model = new CommunityDistrictModel();
        $list = $model->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //街道列表
    public function street(){
        $model = new CommunityZoneModel();
        $list = $model->with('district')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加修改街道页面
    public function street_add(){
        $id=input('id');
        $district=Db::name('community_district')->select();
        if($id){
            $data=Db::name('community_zone')->where('id',$id)->find($id);
            $this->assign('data',$data);
            $this->assign('id',$id);
        }else{
            $this->assign('data',['district_id'=>0]);
        }
        $this->assign('district',$district);
        return $this->fetch();
    }
    //保存添加修改街道数据
    public function street_save(){
        $Model = new CommunityZoneModel();
        $data=input('post.');
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminOrganize/street'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminOrganize/street'));
        }
    }
    //删除街道
    public function street_del(){
        $Model = new CommunityZoneModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }

    //社区列表
    public function community(){
        $model = new CommunityModel();
        $list = $model->with('zone')->paginate(20);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch();
    }
    //添加修改社区页面
    public function community_add(){
        $id=input('id');
        $street=Db::name('community_zone')->select();
        if($id){
            $data=Db::name('community')->where('id',$id)->find($id);
            $this->assign('data',$data);
            $this->assign('id',$id);
        }else{
            $this->assign('data',['zone_id'=>0]);
        }
        $this->assign('street',$street);
        return $this->fetch();
    }

    //保存添加修改街道数据
    public function community_save(){
        $Model = new CommunityModel();
        $data=input('post.');
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminOrganize/community'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminOrganize/community'));
        }
    }
    //删除大队
    public function big_team_del(){
        $id= $this->request->param('id');
        $result=Db::name('organize')->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }


    //中队管理
    public function group(){
        $db=new OrganizeChildModel();
        $data=$db->with('organize')->select();
        $this->assign('list',$data);
        return $this->fetch();
    }
    //添加。修改中队页面
    public function child_add(){
        $Model = new OrganizeChildModel();
        $id=input('id');
        $org=Db::name('organize')->field('id,name')->select();
        if($id){
            $data=$Model->find($id);
            $this->assign('id',$id);
        }else{
            $data['org_id']=1;
        }
        $this->assign('data',$data);
        $this->assign('org',$org);
        return $this->fetch();
    }
    //保存添加 修改团体数据页面
    public function child_save(){
        $Model = new OrganizeChildModel();
        $data=input('post.');
//        dump($data);exit;
        if(empty($data['id'])){
            unset($data['id']);
            $result = $Model->insert($data);
            if ($result === false) {
                $this->error('添加失败!');
            }
            $this->success('添加成功!', url('AdminOrganize/group'));
        }else{
            $result = $Model->update($data);
            if ($result === false) {
                $this->error('修改失败!');
            }
            $this->success('修改成功!', url('AdminOrganize/group'));
        }
    }
    //删除志愿者组织分支
    public function child_del(){
        $Model = new OrganizeChildModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //志愿者成员
    public function member(){
        $db=new UserOrganizeModel();
        $id=input('id');
        $org=Db::name('organize')->where('id',$id)->value('name');
        $data=$db->with(['rank','user'])->where('org_id',$id)->order('status asc')->order('id desc')->paginate(15);
        //dump($data);exit;
        $status=['<p style="color: red">申请中</p>','<p style="color: green">已加入</p>'];
        $this->assign('status',$status);
        $this->assign('id',$id);
        $this->assign('org',$org);
        $this->assign('data',$data);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    //同意申请
    public function agree(){
        $id=input('id');
        $org_id=input('org_id');
        $data=['status'=>1];
        $model=new UserOrganizeModel();
        $res=$model->where('id',$id)->update($data);
        if ($res === false) {
            $this->error('通过失败!');
        }
        $this->success('通过成功!', url('AdminOrganize/member',['id'=>$org_id]));
    }
    //批量通过
    public function all_agree(){
        $ids     = $this->request->param('ids/a');
        $result  = Db::name('user_organize')->where('id', 'in', $ids)->update(['status'=>1]);
        if ($result) {
            $this->success("通过成功！", '');
        } else {
            $this->error('通过失败');
        }
    }
    //拉黑
    public function cancel()
    {
        $id=input('id');
        $org_id=input('org_id');
        $data=['status'=>0,'talent'=>0];
        $model=new UserOrganizeModel();
        $res=$model->where('id',$id)->update($data);
        if ($res === false) {
            $this->error('拉黑失败!');
        }
        $this->success('拉黑成功!', url('AdminOrganize/member',['id'=>$org_id]));
    }
    //批量拉黑
    public function all_cancel(){
        $ids     = $this->request->param('ids/a');
        $result  = Db::name('user_organize')->where('id', 'in', $ids)->update(['status'=>0]);
        if ($result) {
            $this->success("拉黑成功！", '');
        }
    }

    //删除志愿者成员
    public function member_del(){
        $Model = new UserOrganizeModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //批量删除
    public function all_del(){
        $ids     = $this->request->param('ids/a');
        $result  = Db::name('user_organize')->where('id', 'in', $ids)->delete();
        if ($result) {
            $this->success("删除成功！", '');
        } else {
            $this->error('删除失败');
        }
    }

    //加为明星交管员
//    public function talent_add(){
//        $id=input('id');
//        $org_id=input('org_id');
//        $data=['talent'=>1];
//        $model=new UserOrganizeModel();
//        $res=$model->where('id',$id)->update($data);
//        if ($res === false) {
//            $this->error('添加失败!');
//        }
//        $this->success('添加成功!', url('AdminOrganize/member',['id'=>$org_id]));
//    }
    //撤销明星交管员
//    public function talent_cancel()
//    {
//        $id=input('id');
//        $org_id=input('org_id');
//        $data=['talent'=>0];
//        $model=new UserOrganizeModel();
//        $res=$model->where('id',$id)->update($data);
//        if ($res === false) {
//            $this->error('撤销失败!');
//        }
//        $this->success('撤销成功!', url('AdminOrganize/member',['id'=>$org_id]));
//
//    }



}